package umacis.data;

import java.util.Enumeration;
import java.util.NoSuchElementException;
import java.util.Vector;

import umacis.data.elements.Key;




/**
 * @author Administrator
 *
 */
/**
 * @author ptw
 *
 */
public class SystemKeys  {
	private static SystemKeys INSTANCE = new SystemKeys();
	private Vector vectorOfKeys;
	
	public SystemKeys() {
		vectorOfKeys = new Vector();
		initKeyCodes();
		
	}
	
	/**
	 * adds phoneKey to vectorOfKeys if its keyCode is not in it yet.
	 * @param phoneKey if keyCode of phoneKey exists it does nothing, otherwise it adds phoneKey to vectorOfKeys
	 * @return index of phoneKey in vectorOfKeys
	 */
	public int setKey(Key key) {
		int index = 0;
		for (Enumeration e = vectorOfKeys.elements() ; e.hasMoreElements() ;) {
			 Key keyToCompare = (Key) e.nextElement();
	         if ( key.code==keyToCompare.code)  {
	        	 //vectorOfKeys.setElementAt(;, index)
	        	 return index;
	         }
	         else index++;
		}
		vectorOfKeys.addElement(key); 
		return index;
		
	}
	
	
	public int setKeyName(Key keyToSet) {
		int index = 0;
		for (Enumeration e = vectorOfKeys.elements() ; e.hasMoreElements() ;) {
			 Key key = (Key) e.nextElement();
	         if ( key.code==keyToSet.code ) {
	        	 key.name = keyToSet.name;
	        	 return index;
	         }
	         else index++;
		}
		vectorOfKeys.addElement(keyToSet); 
		return index;
		
	}
	
	public int setKeyName(int code, String nameToSet) {
		int index = 0;
		for (Enumeration e = vectorOfKeys.elements() ; e.hasMoreElements() ;) {
			 Key key = (Key) e.nextElement();
	         if ( key.code==code ) {
	        	 key.name = nameToSet;
	        	 return index;
	         }
	         else index++;
		}
		vectorOfKeys.addElement(new Key(code, nameToSet)); 
		return index;
		
	}
	
	public int getNumberOfKeys() {
		return vectorOfKeys.size();
	}
	
	public void removeKey(Key key) {
	
		int i=0;
		for (Enumeration e = vectorOfKeys.elements() ; e.hasMoreElements() && ! ((Key)e.nextElement()).equals(key);) i++;
		if (i<vectorOfKeys.size()) vectorOfKeys.removeElementAt(i);
	   
	}
	
	public void removeAllKeys() {
		vectorOfKeys.removeAllElements();
	}
	
	public void renameKey(int code, String newName) {
		Key key;
		for (Enumeration e = vectorOfKeys.elements() ; e.hasMoreElements() ;) {
	         if ( (key = (Key) e.nextElement()).code == code ) key.name = newName;
		}
	}
	/**
	 * this finds the key and returns it, if not found it returns a new key, after adding it to the vector first
	 * @param code
	 * @return key
	 */
	public Key getKeyObject (int code) {
		Key key;
		for (Enumeration e = vectorOfKeys.elements() ; e.hasMoreElements() ;) {
	         if ( (key = (Key) e.nextElement()).code == code ) return key;
		}
		key =  new Key(code,  String.valueOf(code));
		vectorOfKeys.addElement(key);
		return key;
	}
	
	public int getKeyIndex (int code) {
		int index = 0;
		for (Enumeration e = vectorOfKeys.elements() ; e.hasMoreElements() ;) {
	         if ( ((Key) e.nextElement()).code == code ) return index;
	         else index++;
		}
		return -1;
	}
	
	//private SystemKeys keys = new SystemKeys();
	public  Key getSystemKey(int keyCode) {
		return getKeyObject(keyCode);
	}
	
	private void initKeyCodes() {
    	for (int i=0; i<Key.CODES.length; i++) setKeyName(Key.CODES[i],Key.NAMES[i]);
		}

	
//	public String getNameOfKey (int code) {
//		Key key;
//		for (Enumeration e = vectorOfKeys.elements() ; e.hasMoreElements() ;) {
//	         if ( (key = (Key) e.nextElement()).code == code ) return key.name;
//		}
//		return "ERROR: KEY DOESN'T EXIST !!"; 
//	}
	
//	public Key getObjectOfKeyFromName(String name) {
//		Key k;
//		for (Enumeration e = vectorOfKeys.elements() ; e.hasMoreElements() ;) {
//			if ( (k = (Key) e.nextElement()).name.equals(name) ) return k;
//		}
//		return  null; //XXX is this right??
//	}
	
//	public boolean isExistName(String name) {
//		for (Enumeration e = vectorOfKeys.elements() ; e.hasMoreElements() ;) {
//	         if ( ((Key) e.nextElement()).name.equals(name) ) return true;
//		}
//		return false; //XX is this right??
//	}
	
	
	
	public Enumeration elements() {
		return new Enumeration() {
		    int count = 0;

		    public boolean hasMoreElements() {
			return count < vectorOfKeys.size();
		    }

		    public Object nextElement() {
			 {
			    if (count < vectorOfKeys.size()) {
				return (Key)vectorOfKeys.elementAt(count++);
			    }
			}
			throw new NoSuchElementException("Vector Enumeration");
		    }
		};
	}

//	public boolean moveKey(int fromIndex, int toIndex) {
////		int index = vectorOfKeys.indexOf(key);
//		int size = vectorOfKeys.size();
//		
//		if (fromIndex < 0 || fromIndex > size-1 || toIndex < 0 || toIndex > size ) return false;
//		vectorOfKeys.removeElementAt(fromIndex);  
//		vectorOfKeys.insertElementAt(toIndex);
//		else return false;
//		return true;
//		
//		
//	}
//	
//	public boolean moveKeyUp(Key key) {
//		int index = vectorOfKeys.indexOf(key);
//		//int size = vectorOfKeys.size();
//		if (index == -1 || index == 0) return false;
//		if (vectorOfKeys.removeElement(key) ) vectorOfKeys.insertElementAt(key, index-1);
//		else return false;
//		return true;
//		
//	}

	public void insertKey(Key selectedKey, int i) {
		vectorOfKeys.insertElementAt(selectedKey, i);
		
	}

	public static SystemKeys getInstance() {
		return INSTANCE ;
	}

}

	
